//
//  BoardDetaillListController.m
//  Runone
//
//  Created by runone01 on 16/8/11.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "BoardDetaillListController.h"
#import "BoardDetailListCell.h"
#import "DeviceBaseInfo.h"
#import "PopView.h"
#import "LewPopupViewController.h"
#import "UIView+Message.h"
#import "DeviceBaseInfo.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "DMCMSCurrentDataInfo.h"
#import "ContentViewController.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "AppLocalManager.h"

@class VideoSetting;
@interface BoardDetaillListController ()<UITableViewDataSource, UITableViewDelegate,UIPopoverPresentationControllerDelegate>
@property(nonatomic,strong)UIImageView *imgV;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)NSTimer *timer;
@property (nonatomic, strong)PopView *popV;
@property(nonatomic,strong)UIView *Tview;
@property (strong, nonatomic)DeviceBaseInfo *Camera;
@property (strong, nonatomic)DMCMSCurrentDataInfo *board;
@property(nonatomic,strong)ContentViewController *contentVC;
@property (nonatomic, strong) NSArray *info;
@end
static NSString *const cellId =@"cell";
@implementation BoardDetaillListController
{
    UITableView *boderTalbieView;
}

#pragma -------------情报板列表主界面


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"情报板详情";
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text          = @"情报板列表";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor     = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    //    _contentView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    
    _contentView.alpha=1;
    _imgV=[[UIImageView alloc] initWithFrame:CGRectMake(30, 240, self.view.frame.size.width-60,214)];
    _imgV.backgroundColor=[UIColor greenColor];
    _imgV.image=[UIImage imageNamed:@"message.png"];
    
    boderTalbieView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    boderTalbieView.dataSource = self;
    boderTalbieView.delegate = self;
    boderTalbieView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    boderTalbieView.bounces  = NO;
    [boderTalbieView registerNib:[UINib nibWithNibName:@"BoardDetailListCell" bundle:nil] forCellReuseIdentifier:cellId];
    boderTalbieView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:boderTalbieView];
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

-(void)request {
    
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetDeviceDataURL;
    getCurrentEventModel.DataValue= [self DataValueStr:_Camera.DeviceUID];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"GetDeviceSettingInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        NSLog(@"%@",requestOutginseng.DataValue);
        NSDictionary *dic=[requestOutginseng.DataValue mj_JSONObject];
        _info = [[dic objectForKey:@"DeviceData"] mj_JSONObject];
//        _contentVC=[[ContentViewController alloc] init];
//        _contentVC.messageArray=info;
        
        _contentView=MessageViewFrame;
        if (_info.count>0) {
            [self picker];
        }
        else {
            // [SVProgressHUD showErrorWithStatus:@"情报板暂无数据"];
            
            [_contentView prompt:@" 情报板暂无内容!~ "];
            //  [_contentView DeviceMessage:@"情报板暂无内容"];
            [self.view addSubview:_contentView];
        }
        
        NSLog(@"%@",_board.TEXT_INFO);
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    _contentView.hidden=YES;
    [_contentView removeFromSuperview];
    //  [_contentVC dismissViewControllerAnimated:YES completion:nil];
}
-(NSString *)DataValueStr:(NSString *)str {
    NSString *beginstr=[NSString stringWithFormat:@"[{\"key\":\"DeviceUID\",\"value\":\"%@\"}]",str];
    
    return beginstr;
}
#pragma  mark - tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BoardDetailListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[BoardDetailListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        //cell.selectionStyle    = UITableViewCellSelectionStyleNone;
    }
    [cell setHighlighted:NO];
    DeviceBaseInfo *deviceBaseInfo = self.contentArray[indexPath.row];
//    cell.pileNoLabel.text = [NSString stringWithFormat:@"%@+%ld",deviceBaseInfo.PileNo,(long)deviceBaseInfo.PileDistance];
    
    //广乐特殊的要求，只能写死
    if ([_currentRoadModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
        cell.pileNoLabel.text = [NSString stringWithFormat:@"%@+%ld",deviceBaseInfo.PileNo,(long)deviceBaseInfo.PileDistance];
    } else {
        NSString *LStr  = @"L";
        NSString *LKStr = [LStr stringByAppendingString:deviceBaseInfo.PileNo];
        cell.pileNoLabel.text  = [NSString stringWithFormat:@"%@+%ld",LKStr,(long)deviceBaseInfo.PileDistance];
    }

    cell.derectionLabel.text     =  deviceBaseInfo.DirectionName;
    //cell.selectionStyle          = UITableViewCellSelectionStyleNone;
    NSString *s=@"Bearer ";
    NSString *strone;
    SDWebImageDownloader *sdmanager = [SDWebImageManager sharedManager].imageDownloader;
    if (GETACCESS_TOKEN!=nil) {
        // NSLog(@"%@",GETACCESS_TOKEN);
        strone=[s stringByAppendingString:GETACCESS_TOKEN];
        [sdmanager setValue:strone forHTTPHeaderField:@"Authorization"];
    }

    NSString * str = _currentRoadModel.RoadPicUrl;
    [cell.userIconImage sd_setImageWithURL:[str mj_url]];
    NSString *contenStr = [deviceBaseInfo.DeviceData substringWithRange:NSMakeRange(12, 9)];
    cell.tipInfoLabel.text = contenStr;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _Camera = self.contentArray[indexPath.row];
    [self request];
    
}

-(void)picker {
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    contentVC.messageArray           = _info;
    contentVC.modalPresentationStyle = UIModalPresentationPopover;
    contentVC.popoverPresentationController.sourceView = self.view;
    contentVC.popoverPresentationController.sourceRect = CGRectMake(self.view.center.x-120, self.view.center.y, 0, 0);
    contentVC.popoverPresentationController.permittedArrowDirections=NO;
    contentVC.popoverPresentationController.delegate = self;
    [self presentViewController:contentVC animated:YES completion:nil];
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}


@end
